//
//  MyViewController.m
//  Hai
//
//  Created by 千金贷 on 16/2/23.
//  Copyright © 2016年 wangzijian. All rights reserved.
//

#import "BoundPhoneController.h"
#import "StepAccountViewController.h"

@interface BoundPhoneController()

@end

@implementation BoundPhoneController{

AFHTTPRequestOperationManager *_manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _manager = [AFHTTPRequestOperationManager manager];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"帐户设置";
    UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:_timeButton.bounds
                                                    byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight)
                                                          cornerRadii:CGSizeMake(5.f, 5.f)];
    _cptext.keyboardType = UIKeyboardTypeNumberPad;
    // Create the shape layer and set its path
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = _timeButton.bounds;
    maskLayer.path = maskPath.CGPath;
    // Set the newly created shape layer as the mask for the image view's layer
    _timeButton.layer.mask = maskLayer;
    [_timeButton.layer setMasksToBounds:YES];
    _telView.layer.borderColor = ButtonGray.CGColor;
    _cpView.layer.borderColor = ButtonGray.CGColor;
   
}
- (void)viewWillAppear:(BOOL)animated{
    [ super viewWillAppear:animated];
}
- (IBAction)next{
//    UpdatePwdController *u = [[UpdatePwdController alloc] init];
//    [self.navigationController pushViewController:u animated:YES];
    StepAccountViewController *user = [[StepAccountViewController alloc]init];
    [self.navigationController pushViewController:user animated:YES];
}
 
- (IBAction)startTime{
    
    __block int timeout=59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                _timeButton.backgroundColor = PinkColor;
                [_timeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                _timeButton.userInteractionEnabled = YES;
                
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                ////NSLog(@"____%@",strTime);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                _timeButton.backgroundColor = ButtonGray;
                [_timeButton setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                _timeButton.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
    NSString *url=[NSString stringWithFormat:@"%@sms/sendCode",BaseURL];
    NSDictionary *parameters = @{@"telnum":_teltext.text,@"codeType":@1};
    AFHTTPRequestOperation*op= [_manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError*error;
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        //NSLog(@"%@",dic);
        NSString *result = dic[@"code"];
        if ([result isEqualToString:@"4000"]) {
            [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"验证码已发送至%@****%@",[_teltext.text substringToIndex:3],[_teltext.text substringFromIndex:7]] message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            _teltext.userInteractionEnabled = NO;
        }else if ([result isEqualToString:@"1207"]){
            [[[UIAlertView alloc] initWithTitle: @"发送失败，请稍后再试" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                _timeButton.backgroundColor = PinkColor;
                [_timeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                _timeButton.userInteractionEnabled = YES;
                
            });
            return;
            
        }else if ([result isEqualToString:@"1019"]){
            [[[UIAlertView alloc] initWithTitle: @"手机号已注册" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                _timeButton.backgroundColor = PinkColor;
                [_timeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                _timeButton.userInteractionEnabled = YES;
                
            });
            return;
            
        }else if ([result isEqualToString:@"1017"]){
            [[[UIAlertView alloc] initWithTitle: @"手机号不能为空" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                _timeButton.backgroundColor = PinkColor;
                [_timeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                _timeButton.userInteractionEnabled = YES;
                
            });
            return;
            
        }else if([result isEqualToString:@"1018"]){
            [[[UIAlertView alloc] initWithTitle: @"手机号格式错误" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                _timeButton.backgroundColor = PinkColor;
                [_timeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                _timeButton.userInteractionEnabled = YES;
                
            });
            return;
            
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [[[UIAlertView alloc] initWithTitle: @"网络异常" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
        NSLog(@"网络异常，error = %@",error.localizedDescription);
        return;
    }];
    op.responseSerializer = [AFCompoundResponseSerializer serializer];
    [op start];
    
}


@end
